/*
		Designed by: SELECTO
		Original image: https://dribbble.com/shots/5311359-Diprella-Login
*/

let switchCtn = document.querySelector("#switch-cnt");
let switchC1 = document.querySelector("#switch-c1");
let switchC2 = document.querySelector("#switch-c2");
let switchCircle = document.querySelectorAll(".switch__circle");
let switchBtn = document.querySelectorAll(".switch-btn");
let aContainer = document.querySelector("#a-container");
let bContainer = document.querySelector("#b-container");
let allButtons = document.querySelectorAll(".submit");

let getButtons = (e) => e.preventDefault()

let changeForm = (e) => {

    switchCtn.classList.add("is-gx");
    setTimeout(function(){
        switchCtn.classList.remove("is-gx");
    }, 1500)

    switchCtn.classList.toggle("is-txr");
    switchCircle[0].classList.toggle("is-txr");
    switchCircle[1].classList.toggle("is-txr");

    switchC1.classList.toggle("is-hidden");
    switchC2.classList.toggle("is-hidden");
    aContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-txl");
    bContainer.classList.toggle("is-z200");
}

let mainF = (e) => {
    for (var i = 0; i < allButtons.length; i++)
        allButtons[i].addEventListener("click", getButtons );
    for (var i = 0; i < switchBtn.length; i++)
        switchBtn[i].addEventListener("click", changeForm)
}

window.addEventListener("load", mainF);

// 定义模态框
let dialog_username_password_missing =  new mModal({ // 登录用户名或密码输入不完整
    content:"请输入完整用户信息后认证！",
    title:"参数不完整",
    confirm:function () {
        dialog_username_password_missing.close();
    }
})
let dialog_admin_login_successful =  new mModal({ // 登录用户成功
    content:"用户认证成功，欢迎管理员回来！2秒后跳转...",
    title:"认证结果",
    confirm:function () {
        dialog_admin_login_successful.close();
    }
})
let dialog_admin_login_error =  new mModal({ // 登录用户失败
    content:"用户登录失败,请检查您的用户信息无误后联系管理员！",
    title:"认证结果",
    confirm:function () {
        dialog_admin_login_error.close();
    }
})


// Ajax提交

// 登录验证按钮单击
$("#btn_AuthenticationAdmin").click(function () {
    let inputtedUserName = $("#input_username").val();
    let inputtedPassword = $("#input_password").val();
    // 数据效验
    if(inputtedUserName === "" || inputtedPassword === ""){
        dialog_username_password_missing.renderDom();
        return
    }
    $("#btn_AuthenticationAdmin").text("认证中...")
    $.ajax({
        url:"/admin/login",
        type:"POST",
        data:JSON.stringify({
            "userName":inputtedUserName,
            "password":inputtedPassword
        }),
        contentType:"application/json; charset=UTF-8",
        dataType:"json",
        success:function (data,status,xhr) {
            $("#btn_AuthenticationAdmin").text("认证身份")
            if (data.code === 200){
                // 登录成功
                dialog_admin_login_successful.renderDom();
                setTimeout(function () {
                    window.location = "../admin/PanelIndex.html";
                },3000)
            } else {
                // 登录失败
                dialog_admin_login_error.renderDom();
            }
        }
    })
})